package cn.imjzm.runcarserve.service.impl;

import cn.imjzm.runcarserve.service.AudioService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

@Service
public class AudioServiceImpl implements AudioService {

    @Override
    public void playAudio() {
        try {
            // FastAPI 服务地址（替换成你树莓派实际 IP）
            String urlString = "http://192.168.0.123:8000/play";  // 修改为实际地址
            URL url = new URL(urlString);

            // 发起 HTTP 请求
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");  // 或 POST，看 FastAPI 的实现

            // 读取 FastAPI 返回内容（可选）
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream())
            );
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("[FastAPI] " + line);
            }

            reader.close();
            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("[Error] 无法连接 FastAPI 接口");
        }
    }
}
